﻿using System;
namespace Validate.Net
{
    /// <summary>
    /// 数字类型最小值校验
    /// </summary>
    public class ValidateMinValueAttribute : AbstractValidateAttribute
    {
        private double minValue;

        public ValidateMinValueAttribute(double minValue)
        {
            this.minValue = minValue;
        }

        public override bool Validate(object oValue, out string msg)
        {
            Type type = oValue?.GetType();
            if (!object.ReferenceEquals(typeof(int), type) &&
                !object.ReferenceEquals(typeof(long), type) &&
                !object.ReferenceEquals(typeof(double), type))
            {
                msg = $"特性[{this.GetType().Name}]只能校验数字类型";
                return false;
            }
            if (double.TryParse(oValue?.ToString(), out double value)
                     && value >= this.minValue)
            {
                msg = string.Empty;
                return true;
            }
            else
            {
                msg = $"最小值为：{this.minValue}";
                return false;
            }
        }
    }
}
